﻿using System;
using System.Collections.Generic;

using System.Text;
using Estimate.Common.DataImporter;
using System.IO;

namespace Estimate.Common.DataFileCopyer
{
    /// <summary>
    /// 文件拷贝基类。子类可以将指定的文件拷贝到另外一个目录中去
    /// </summary>
    /// 
    public class BaseDataFileCopyer
    {
        protected string _baseDir;
        public BaseDataFileCopyer(string baseDir)
        {
            _baseDir = baseDir;
            CheckAndCreateDirecotry(baseDir);
        }

        /// <summary>
        /// 从importer中获取文件列表，并拷贝
        /// </summary>
        /// <param name="pathList">文件列表</param>
        public virtual void copyFiles(string[] pathList)
        {
            foreach (string path in pathList)
            {
                if (path.Trim().Length == 0)
                    continue;
                foreach (string newPath in this.getCopyDir(path))
                {
                    CheckAndCreateDirecotry(new DirectoryInfo(newPath).Parent.FullName);
                    try
                    {
                        System.IO.File.Copy(path, newPath, true);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }
            }
        }

        /// <summary>
        /// 获取变更后的路径
        /// </summary>
        /// <param name="path">原路径</param>
        /// <returns>变更上的路径</returns>
        public virtual string[] getCopyDir(string path)
        {
            return new string[] { _baseDir + System.IO.Path.GetFileName(path) };
        }

        public void CheckAndCreateDirecotry(string path)
        {
            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }
        }
    }
}
